package com.ruoyi.web.controller.system.assets;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.config.HeadNum;
import com.ruoyi.common.core.domain.Ajax2Result;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.GetSessionValueUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.assets.FzBuildingUnit;
import com.ruoyi.system.domain.assets.FzBusinessFloor;
import com.ruoyi.system.domain.communiry.FzSCommunity;
import com.ruoyi.system.service.assets.IFzBuildingUnitService;
import com.ruoyi.system.service.assets.IFzBusinessFloorService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpSession;

/**
 * 单元信息Controller
 * 
 * @author ruoyi
 * @date 2020-02-28
 */
@Controller
@RequestMapping("/system/unit")
public class FzBuildingUnitController extends BaseController
{
    private String prefix = "system/assets/unit";
    @Autowired
    private IFzBusinessFloorService fzBusinessFloorService;
    @Autowired
    private IFzBuildingUnitService fzBuildingUnitService;

    @RequiresPermissions("system:unit:view")
    @GetMapping()
    public String unit(ModelMap map,HttpSession session)
    {
        String communityId=((FzSCommunity)session.getAttribute("ycommunity")).getCommunityId();
        FzBusinessFloor fzBusinessFloor=new FzBusinessFloor();
        fzBusinessFloor.setcId(communityId);
        List<FzBusinessFloor> list = fzBusinessFloorService.selectFzBusinessFloorList(fzBusinessFloor);
        map.put("floorSelect",list);
        return prefix + "/unit";
    }

    /**
     * 查询单元信息列表
     */
    @RequiresPermissions("system:unit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpSession session,FzBuildingUnit fzBuildingUnit)
    {
        startPage();
        String communityId=((FzSCommunity)session.getAttribute("ycommunity")).getCommunityId();
        fzBuildingUnit.setcId(communityId);
        List<FzBuildingUnit> list = fzBuildingUnitService.selectFzBuildingUnitList(fzBuildingUnit);
        return getDataTable(list);
    }

    /**
     * 导出单元信息列表
     */
    @RequiresPermissions("system:unit:export")
    @Log(title = "单元信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FzBuildingUnit fzBuildingUnit)
    {
        List<FzBuildingUnit> list = fzBuildingUnitService.selectFzBuildingUnitList(fzBuildingUnit);
        ExcelUtil<FzBuildingUnit> util = new ExcelUtil<FzBuildingUnit>(FzBuildingUnit.class);
        return util.exportExcel(list, "unit");
    }

    /**
     * 新增单元信息
     */
    @GetMapping("/add")
    public String add(ModelMap map,HttpSession session)
    {
        String communityId=((FzSCommunity)session.getAttribute("ycommunity")).getCommunityId();
        FzBusinessFloor fzBusinessFloor=new FzBusinessFloor();
        fzBusinessFloor.setcId(communityId);
        List<FzBusinessFloor> list = fzBusinessFloorService.selectFzBusinessFloorList(fzBusinessFloor);
        map.put("floorSelectAdd",list);
        return prefix + "/add";
    }

    /**
     * 新增保存单元信息
     */
    @RequiresPermissions("system:unit:add")
    @Log(title = "单元信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FzBuildingUnit fzBuildingUnit)
    {
        fzBuildingUnit.setUnitId(HeadNum.UNIT_HEAD+ DateUtils.getTimeMillis());
        fzBuildingUnit.setcId(GetSessionValueUtil.getCommunity());
        fzBuildingUnit.setCreateBy(ShiroUtils.getSysUser().getUserName());
        return toAjax(fzBuildingUnitService.insertFzBuildingUnit(fzBuildingUnit));
    }

    /**
     * 修改单元信息
     */
    @GetMapping("/edit/{unitId}")
    public String edit(@PathVariable("unitId") String unitId, ModelMap mmap)
    {
        FzBuildingUnit fzBuildingUnit = fzBuildingUnitService.selectFzBuildingUnitById(unitId);
        mmap.put("fzBuildingUnit", fzBuildingUnit);
        return prefix + "/edit";
    }

    /**
     * 修改保存单元信息
     */
    @RequiresPermissions("system:unit:edit")
    @Log(title = "单元信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FzBuildingUnit fzBuildingUnit)
    {
        fzBuildingUnit.setUpdateBy(ShiroUtils.getSysUser().getUserName());
        return toAjax(fzBuildingUnitService.updateFzBuildingUnit(fzBuildingUnit));
    }

    /**
     * 删除单元信息
     */
    @RequiresPermissions("system:unit:remove")
    @Log(title = "单元信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fzBuildingUnitService.deleteFzBuildingUnitByIds(ids));
    }


//查询单元信息
    @GetMapping("/unitById/{unitId}")
    @ResponseBody
    public Ajax2Result selectfloorById(@PathVariable("unitId") String unitId)
    {
        FzBuildingUnit fzBuildingUnit = fzBuildingUnitService.selectFzBuildingUnitById(unitId);
        List<FzBuildingUnit>  fzBuildingUnits= Arrays.asList(fzBuildingUnit);
        return Ajax2Result.success(fzBuildingUnits);
    }
}
